$(function(){

    // 第一次渲染部门
    $.ajax({
        url:'/Admin/Org/index',
        type:'get',
        success:function(data){
            "use strict";
            var html = ``;
            for(var i=0;i<data.length;i++){
                if(data[i].level === 1){
                    html += `
                        <li class="companyDepartmentList" add_id="${data[i].id}">
                            <div class="companyDepartmentList_tip">
                                <p class="companyDepartmentList_tip_text"><span class="DepartmentName_text">${data[i].dep_name}</span> (<span class="Department_num">${data[i].staffs}</span>人) <i class="iconfont icon-bj"></i> <i class="iconfont icon-105"></i></p>
                                <p class="DepartmentLOGO"><i class="iconfont icon-jiantouyou jiantou_logo"></i></p>
                            </div>
                            <div class="companyDepartmentList_hidden">
                                <a class="btn btn-sm btn-success" href="#add_DepartmentModal_zi">添加子部门</a>
                                <ul class="companyDepartmentBox_zi">
                        `
                    if(data[i].child_list){
                        for(var j=0;j<data[i].child_list.length;j++){
                            html += `
                                <li class="companyDepartmentList_zi" add_id="${data[i].child_list[j].id}">
                                     <p class="companyDepartmentList_tip_text"><span class="DepartmentName_text">${data[i].child_list[j].dep_name}</span> (<span class="Department_num">${data[i].child_list[j].staffs}</span>人) <i class="iconfont icon-bj"></i> <i class="iconfont icon-105"></i></p>
                                </li>
                            `
                        }
                    }
                    html += `
                                </ul>
                            </div>
                        </li>
                    `
                }
            }
            $('#companyDepartmentBox').html(html);
        }
    })

    /*一级部门添加*/
    $('#addDepartment_btn').click(function(){
        "use strict";
        var showBox = $(this).attr('href');
        $(showBox).addClass('active')
    })
    $('#add_btn').click(function(){
        addDepartment_Name()
        // $(this).closest('.add_hiddenBox').removeClass('active')
        $('#add_btn').closest('.add_hiddenBox').removeClass('active')
    })
    $('#add_AddNew_btn').click(function(){
        addDepartment_Name()
    })
    $('.button_list').on('click','.cancel_btn',function(){
        "use strict";
        $(this).closest('.add_hiddenBox').removeClass('active')
    })

    // 编辑、删除部门
    $('.companyDepartmentBox').on('click','li .companyDepartmentList_tip_text i',function(e){
        "use strict";
        e.preventDefault();
        e.stopPropagation();
        if($(this).hasClass('icon-bj')){
            // console.log('编辑')
            var DepartmentName = $(this).siblings('.DepartmentName_text').text()
            $('#edit_DepartmentModal').find('input[add_name="addDepartment_Name"]').val(DepartmentName)
            $('#edit_DepartmentModal').addClass('active')
            window.sessionStorage['edit_id']=$(this).closest('li').attr('add_id')
        }else if($(this).hasClass('icon-105')){
            let li_LIST_length = $(this).closest('ul').children('li').length;
            // console.log('删除')
            if(Number($(this).siblings('.Department_num').text())>0){
                $('#delete_departMent').addClass('active')
            }else if(li_LIST_length<=1){
                $('#yz_modal').html('不能在删除了！')
                $('#yz_modal').addClass('active')
                tipShow()
            }else{
                let id =  $(this).closest('li').attr('add_id');
                var that = $(this);
                $.ajax({
                    url:'/Admin/Org/hasStaff',
                    type:'get',
                    data:{
                        id:id
                    },
                    success:function(data){
                        if(data.code==1){
                            that.closest('li').remove()
                        }
                    }
                })

            }
        }
    })
    /**
     * 编辑部门名称框保存时
     */
    $('.button_list').on('click','button',function(){
        "use strict";
        if($(this).attr('id')=='save_btn'){
            let edit_id = window.sessionStorage['edit_id']
            console.log(edit_id)
            let Department_Name = $(this).closest('.add_hiddenBox').find('input[add_name="addDepartment_Name"]').val()
            $.ajax({
                url:'/Admin/Org/editDep',
                type:'post',
                data:{
                    dep_name:Department_Name,
                    id:edit_id
                },
                success:function(data){
                    // console.log(data)
                    if(data.code === 1){
                        // console.log($(`li[add_id=${edit_id}]>:first-child`).find('.DepartmentName_text'))
                        $(`li[add_id=${edit_id}]>:first-child`).find('.DepartmentName_text').html(Department_Name)
                        window.sessionStorage['edit_id'] = ''
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                    }
                }
            })
            $(this).closest('.add_hiddenBox').removeClass('active')
        }
    })

    // 部门展开收起起
    $('.companyDepartmentBox').on('click','>li>.companyDepartmentList_tip',function(e){
        e.preventDefault();
        e.stopPropagation();
        console.log(1)
        var add_id = $(this).parent().attr('add_id');
            if($(this).parent().find('i.jiantou_logo').hasClass('icon-jiantouyou')){
                $(this).parent().find('i.jiantou_logo').removeClass('icon-jiantouyou').addClass('icon-jiantouxia');
                $(this).parent().children('.companyDepartmentList_hidden').addClass('active')
            }else{
                $(this).parent().find('i.jiantou_logo').addClass('icon-jiantouyou').removeClass('icon-jiantouxia');
                $(this).parent().children('.companyDepartmentList_hidden').removeClass('active')
            }
        $('.hiddenmodal').attr('id',`addDepartmentModal_zi${add_id}`)
        $(this).parent().children('.companyDepartmentList_hidden').children('button').attr('data-target',`#addDepartmentModal_zi${add_id}`)
        $('.hiddenmodal').find('.modal-footer').children(':nth-child(2)').attr('id',`zi_save_btn${add_id}`)
        window.sessionStorage['add_id'] = add_id
    })
    //哎，二级子菜单
    $('.companyDepartmentBox').on('click','.companyDepartmentList_hidden>a',function(e){
        "use strict";
        e.preventDefault();
        e.stopPropagation();
        var showBox = $(this).attr('href')
        $(showBox).addClass('active')
        let Parent_add_id = $(this).closest('.companyDepartmentList').attr('add_id')
        var dom = $(this).siblings('ul.companyDepartmentBox_zi')
        window.sessionStorage['Parent_add_id'] = $(this).closest('.companyDepartmentList').attr('add_id')
    })
    $('#add_btn_zi').click(function(){
        let Parent_add_id = window.sessionStorage['Parent_add_id'];
        let dom = $(`.companyDepartmentBox>li[add_id=${Parent_add_id}]`).find('ul.companyDepartmentBox_zi');
        addDepartment_Name_zi(Parent_add_id,dom)
        // $(this).closest('.add_hiddenBox').removeClass('active')
        // window.sessionStorage['Parent_add_id'] = ''
    })
    $('#add_AddNew_btn_zi').click(function(){
        let Parent_add_id = window.sessionStorage['Parent_add_id'];
        let dom = $(`.companyDepartmentBox>li[add_id=${Parent_add_id}]`).find('ul.companyDepartmentBox_zi');
        addDepartment_Name_zi(Parent_add_id,dom)
        // $(this).closest('.add_hiddenBox').removeClass('active')
        // window.sessionStorage['Parent_add_id'] = ''
    })
    /*
    * 添加一级部门公共函数
    * */
    function addDepartment_Name(){
        var addDepartment_NameVal = $('#addDepartmentModal').find(`input[add_name="addDepartment_Name"]`).val()
        // var len = $('.companyDepartmentBox>li').length;
        // if(len<1){
        //     len = 1
        // }else{
        //     len+=1
        // }
        if(addDepartment_NameVal!=''){
            $.ajax({
                url:'/Admin/Org/addDep',
                type:'post',
                data:{
                    dep_name:addDepartment_NameVal,
                    pid:0
                },
                success:function(data){
                    "use strict";
                    if(data.code == 1){
                        var companyDepartmentBoxHTML = `
                                <li class="companyDepartmentList" add_id="${data.id}">
                                    <div class="companyDepartmentList_tip">
                                        <p class="companyDepartmentList_tip_text"><span class="DepartmentName_text">${addDepartment_NameVal}</span> (<span class="Department_num">0</span>人) <i class="iconfont icon-bj"></i> <i class="iconfont icon-105"></i></p>
                                        <p class="DepartmentLOGO"><i class="iconfont icon-jiantouyou jiantou_logo"></i></p>
                                    </div>
                                    <div class="companyDepartmentList_hidden">
                                        <a class="btn btn-sm btn-success" href="#add_DepartmentModal_zi">添加子部门</a>
                                        <ul class="companyDepartmentBox_zi">
                                            <li class="companyDepartmentList_zi" add_id="${data.id+1}">
                                                 <p class="companyDepartmentList_tip_text"><span class="DepartmentName_text">${addDepartment_NameVal}</span> (<span class="Department_num">0</span>人) <i class="iconfont icon-bj"></i> <i class="iconfont icon-105"></i></p>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                            `
                        $('.companyDepartmentBox').append(companyDepartmentBoxHTML)
                        $(`input[add_name="addDepartment_Name"]`).val('')
                        // $('#add_btn').closest('.add_hiddenBox').removeClass('active')
                    }else{
                       $('#yz_modal').html(data.msg)
                       $('#yz_modal').addClass('active')
                       tipShow()
                       return
                    }
                }
            })



        }
    }
    /*
    * 添加二级部门公共函数
    * */
    // let len = $(this).siblings('.companyDepartmentBox_zi>li').length;
    function addDepartment_Name_zi(Parent_add_id,dom){
        let addDepartment_NameVal = $('#add_DepartmentModal_zi').find(`input[add_name="addDepartment_Name"]`).val()
        let Parent_len = Parent_add_id
        if(addDepartment_NameVal!=''){
            $.ajax({
                url:'/Admin/Org/addDep',
                type:'post',
                data:{
                    dep_name:addDepartment_NameVal,
                    pid:Parent_len
                },
                success:function(data){
                    "use strict";
                    if(data.code == 1){
                        let HTML = `
                                <li class="companyDepartmentList_zi" add_id="${data.id}">
                                     <p class="companyDepartmentList_tip_text"><span class="DepartmentName_text">${addDepartment_NameVal}</span> (<span class="Department_num">0</span>人) <i class="iconfont icon-bj"></i> <i class="iconfont icon-105"></i></p>
                                </li>
                            `
                        dom.append(HTML)
                        $(`input[add_name="addDepartment_Name"]`).val('')
                        $('#add_btn_zi').closest('.add_hiddenBox').removeClass('active')
                    }else{
                       $('#yz_modal').html(data.msg)
                       $('#yz_modal').addClass('active')
                       tipShow()
                       return
                    }
                }
            })

        }
    }

/************************************************表格啦**************************************************/

    // 修改添加成员里面的radio
    $('.table_btn_list_radio_list').on('click','li',function(e){
        "use strict";
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
    })
    $('#change_department').on('click','.change_departmentList li',function(e){
        "use strict";
        e.preventDefault();
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
    })
    // 添加成员的弹框
    $('.table_btn').on('click','a',function(e){
           "use strict";
           e.preventDefault();
           var showBoxId = $(this).attr('href');
           // console.log(showBoxId)
           if(showBoxId == '#change_department' || showBoxId == '#delete' ){
               var checkboxs =$('input[type=checkbox]:not("#checkAll")');
               var len = 0;
               for (var i = 0; i < checkboxs.length; i++) {
                   if (checkboxs[i].checked) {
                        continue
                   }else{
                       len++
                   }
               }
               if(len == checkboxs.length){
                   $('#yz_modal').html('您还没有选择人员！')
                   $('#yz_modal').addClass('active')
                   tipShow()
                   return
               }else{
                   $(showBoxId).addClass('active');
                   $(showBoxId).siblings('[lll]').removeClass('active')
               }
           }
           if(showBoxId == '#change_department'){
               let html = '';
               $.ajax({
                   url:'/Admin/Org/showDep',
                   type:'get',
                   success:function(data){
                       for(var i=0;i<data.length;i++){
                           html += `
                                <li class="change_departmentList_list">
                                    <i class="iconfont icon-Raidobox"></i>
                                    <span data-id="${data[i].id}">${data[i].dep_name}</span>
                                </li>
                            `
                       }
                       html += `
                           <li class="change_departmentList_btn">
                                <button class="btn btn-sm btn-success">确认调整</button>
                                <button class="btn btn-sm btn-success">取消</button>
                            </li>
                            `
                       $(showBoxId).children('.change_departmentList').html(html)
                       $(showBoxId).children('.change_departmentList').children('li:first-child').children('i').removeClass('icon-Raidobox').addClass('icon-Raidobox-selectedRai')
                   }
               })
           }
           if(showBoxId == '#add_people'){
               var that = $(this);
               let html = '';
               $.ajax({
                   url:'/Admin/Org/showDep',
                   type:'get',
                   success:function(data){
                       for(var i=0;i<data.length;i++){
                           html += `
                                <option value="${data[i].id}">${data[i].dep_name}</option>
                            `
                       }
                       $(showBoxId).find('select[add_people_id="department"]').html(html)
                   }
               })
               $(showBoxId).addClass('active');
               $(showBoxId).siblings('[lll]').removeClass('active')
           }
            if(showBoxId == '#import'){
               // console.log(1)
                // var that = $(this);
                // let html = '';
                // $.ajax({
                //     url:'/Admin/Org/showDep',
                //     type:'get',
                //     success:function(data){
                //         for(var i=0;i<data.length;i++){
                //             html += `
                //                     <option value="${data[i].id}">${data[i].dep_name}</option>
                //                 `
                //         }
                //         $(showBoxId).find('select[add_people_id="department"]').html(html)
                //     }
                // })
                $(showBoxId).addClass('active');
                $(showBoxId).siblings('[lll]').removeClass('active')
                // console.log($('#import').hasClass('active'))
                // if($('#import').hasClass('active')){
                //     //     console.log(111);
                //     $(document).click('not(#import)',function(e){
                //         // if($this.closest('#import').length==0){
                //         // console.log(1)
                //         $('#import').removeClass('active');
                //         // $("#PDFBox").html('')
                //         // }
                //     })
                //
                //
                // }
            }

       })

    // 给表格添加新内容
    $('.add_people_list_btn').on('click','button',function(){
        "use strict";
        if( $(this).html() == '确认添加' ){
            let name = $(this).parent().siblings().find('[add_people_id=name]').val()
            let sex = $(this).parent().siblings().find('[add_people_id=sex]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let num = $(this).parent().siblings().find('[add_people_id=num]').val()
            let department = $(this).parent().siblings().find('[add_people_id=department]').val()
            let departmentText = $(this).parent().siblings().find('[add_people_id=department] option:selected').text()
            let type = $(this).parent().siblings().find('[add_people_id=type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let education = $(this).parent().siblings().find('[add_people_id=education]').val()
            let employType = $(this).parent().siblings().find('[add_people_id=employType]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let title = $(this).parent().siblings().find('[add_people_id=title]').val()
            let post = $(this).parent().siblings().find('[add_people_id=post]').val()
            let major = $(this).parent().siblings().find('[add_people_id=major]').val()
            // let remark = $(this).parent().siblings().find('[add_people_id=remark]').val()

            if(name == '' || sex == '' || num == '' || department == '' || type == '' || education == '' || employType == '' ){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }

            let that = $(this)
            // let peopleTableHTML = $('#table_table').children('tbody').html()
            $.ajax({
                url:'/Admin/Org/addStaff',
                type:'post',
                data:{
                    staff_name:name,
                    staff_sn:num,
                    sex:sex,
                    staff_title:post||'',
                    staff_emp:employType,
                    dep_id:department,
                    staff_type:type,
                    staff_xueli:education,
                    staff_zhicheng:title||'',
                    staff_zy:major||''
                    // staff_bz:remark||''
                },
                success:function(data){
                    if(data.code==1){
                        let peopleTableHTML = `
                        <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="name">${name}</td>
                            <td data-name="sex">${sex}</td>
                            <td data-name="num">${num}</td>
                            <td data-name="type">${type}</td>
                            <td data-name="department">${departmentText}</td>
                            <td data-name="education">${education}</td>
                            <td data-name="employType">${employType}</td>
                            <td data-name="title">${title||'-'}</td>
                            <td  data-name="post">${post||'-'}</td>
                            <td  data-name="major">${major||'-'}</td>
                            <td style><a href="#operation">编辑</a></td>
                        </tr>
            `
                        $('#table_table').children('tbody').prepend(peopleTableHTML)

                        $('[add_people_id=name]').val('')
                        $('[add_people_id=num]').val('')
                        $('[add_people_id=post]').val('')
                        $('[add_people_id=major]').val('')
                        // $('[add_people_id=remark]').val('')
                        that.parent().parent().removeClass('active')
                    }
                }
            })

        }
        if( $(this).html() == '取消' ){
            $(this).parent().parent().removeClass('active')
        }
        if( $(this).html() == '添加并新增' ){

            let name = $(this).parent().siblings().find('[add_people_id=name]').val()
            let sex = $(this).parent().siblings().find('[add_people_id=sex]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let num = $(this).parent().siblings().find('[add_people_id=num]').val()
            let department = $(this).parent().siblings().find('[add_people_id=department]').val()
            let departmentText = $(this).parent().siblings().find('[add_people_id=department] option:selected').text()
            let type = $(this).parent().siblings().find('[add_people_id=type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let education = $(this).parent().siblings().find('[add_people_id=education]').val()
            let employType = $(this).parent().siblings().find('[add_people_id=employType]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            let title = $(this).parent().siblings().find('[add_people_id=title]').val()
            let post = $(this).parent().siblings().find('[add_people_id=post]').val()
            let major = $(this).parent().siblings().find('[add_people_id=major]').val()
            // let remark = $(this).parent().siblings().find('[add_people_id=remark]').val()

            if(name == '' || sex == '' || num == '' || department == '' || type == '' || education == '' || employType == '' ){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }

            let that = $(this)
            // let peopleTableHTML = $('#table_table').children('tbody').html()
            $.ajax({
                url:'/Admin/Org/addStaff',
                type:'post',
                data:{
                    staff_name:name,
                    staff_sn:num,
                    sex:sex,
                    staff_title:post||'',
                    staff_emp:employType,
                    dep_id:department,
                    staff_type:type,
                    staff_xueli:education,
                    staff_zhicheng:title||'',
                    staff_zy:major||''
                    // staff_bz:remark||''
                },
                success:function(data){
                    if(data.code==1){
                        let peopleTableHTML = `
                    <tr data-index="${data.id}">
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td data-name="name">${name}</td>
                            <td data-name="sex">${sex}</td>
                            <td data-name="num">${num}</td>
                            <td data-name="type">${type}</td>
                            <td data-name="department">${departmentText}</td>
                            <td data-name="education">${education}</td>
                            <td data-name="employType">${employType}</td>
                            <td data-name="title">${title||'-'}</td>
                            <td  data-name="post">${post||'-'}</td>
                            <td  data-name="major">${major||'-'}</td>
                            <td style><a href="#operation">编辑</a></td>
                        </tr>
            `
                        $('#table_table').children('tbody').prepend(peopleTableHTML)

                        $('[add_people_id=name]').val('')
                        $('[add_people_id=num]').val('')
                        $('[add_people_id=post]').val('')
                        $('[add_people_id=major]').val('')
                        // $('[add_people_id=remark]').val('')
                        // that.parent().parent().removeClass('active')
                    }
                }
            })
        }
    })
    // 调整部门
    $('#change_department').on('click','.change_departmentList_btn button',function(e){
        "use strict";
        e.stopPropagation();
        e.preventDefault();
        if($(this).html()=='确认调整'){
            let chooseDep = $(this).parent().siblings().children('i.icon-Raidobox-selectedRai').siblings('span').attr('data-id')
            var checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            var change_departmentList_arr = [];
            for (var i = 0; i < checkboxs.length; i++) {
                if (!checkboxs[i].checked) {
                    continue;
                }else{
                    change_departmentList_arr.push( $(checkboxs[i]).parent().parent().attr('data-index') )
                }
            }
            console.log(change_departmentList_arr)
            $.ajax({
                url:'/Admin/Org/adjustStaff',
                type:'post',
                data:{
                    staff_id:change_departmentList_arr,
                    dep_id:chooseDep
                },
                success:function(data){
                    if(data.code==1){
                        console.log('ok')
                        $('#change_department').removeClass('active')
                    }
                }
            })

            
        }else{
            $('#change_department').removeClass('active')
        }
    })
    /**
     *全选的功能
     */
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            
            $.ajax({
                url:'/Admin/Org/multiDelStaff',
                type:'post',
                data:{
                    staff_id:delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                        // console.log('ok')
                        window.location.href = '/Admin/Org/show'//强制刷新
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                        return
                    }

                }
            })
        }else{
            $('#delete').removeClass('active')
        }
    })

    // 表格操作-编辑
    $('#table_table').children('tbody').on('click','a',function(){
        "use strict";
        var operation = $(this).attr('href')
        $(operation).addClass('active')
        let operation_id = $(this).parent().parent().attr('data-index')
        window.sessionStorage['data-index']=operation_id
        let htmls = '';
        $.ajax({
            url:'/Admin/Org/showDep',
            type:'get',
            success:function(data){
                for(var i=0;i<data.length;i++){
                    htmls += `
                                <option value="${data[i].id}">${data[i].dep_name}</option>
                            `
                }
                $('#operation').find('select[add_people_id="department"]').html(htmls)
            }
        })
        $.ajax({
            url:'/Admin/Org/editStaff',
            type:'get',
            data:{id:operation_id},
            success:function(data){
                let datas = data.data;
                if(data.code==1){
                    //姓名
                    $(operation).find('[add_people_id="name"]').val(datas.staff_name)
                    //性别
                    let sexList = $(operation).find('[add_people_id="sex"]').children()
                    for(var sexi=0;sexi<sexList.length;sexi++){
                        var sexVal = $(sexList[sexi]).children('span').html();
                        if(sexVal == datas.staff_name){
                            $(sexList[sexi]).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
                            $(sexList[sexi]).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
                        }
                    }
                    //工号
                    $(operation).find('[add_people_id="num"]').val(datas.staff_sn)
                    //人员类别
                    let typeList = $(operation).find('[add_people_id="type"]').children()
                    for(var tli=0;tli<typeList.length;tli++){
                        var typeVal = $(typeList[tli]).children('span').html();
                        if(typeVal == datas.staff_type){
                            $(typeList[tli]).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
                            $(typeList[tli]).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
                        }
                    }
                    //部门
                    $(operation).find('[add_people_id="department"]').val(datas.dep_id)
                    //学历
                    $(operation).find('[add_people_id="education"]').val(datas.staff_xueli)
                    //聘用方式
                    let employTypeList = $(operation).find('[add_people_id="employType"]').children()
                    for(var empi=0;empi<employTypeList.length;empi++){
                        var employTypeVal = $(employTypeList[empi]).children('span').html();
                        if(employTypeVal == datas.staff_emp){
                            $(employTypeList[empi]).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
                            $(employTypeList[empi]).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
                        }
                    }
                    //职称
                    $(operation).find('[add_people_id="title"]').val(datas.staff_zhicheng)
                    //职务
                    $(operation).find('[add_people_id="post"]').val(datas.staff_title)
                    //专业
                    $(operation).find('[add_people_id="major"]').val(datas.staff_zy)
                    //备注
                    // $(operation).find('[add_people_id="remark"]').val(datas.staff_bz)
                }
            }
        })
    })
    $('.operation_list_btn').on('click','button',function(){
        "use strict";
        if( $(this).html() == '保存' ){
            var name = $(this).parent().siblings().find('[add_people_id=name]').val()
            var sex = $(this).parent().siblings().find('[add_people_id=sex]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            var num = $(this).parent().siblings().find('[add_people_id=num]').val()
            var department = $(this).parent().siblings().find('[add_people_id=department]').val()
            let departmentText = $(this).parent().siblings().find('[add_people_id=department] option:selected').text()
            var type = $(this).parent().siblings().find('[add_people_id=type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            var education = $(this).parent().siblings().find('[add_people_id=education]').val()
            var employType = $(this).parent().siblings().find('[add_people_id=employType]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
            var title = $(this).parent().siblings().find('[add_people_id=title]').val()
            var post = $(this).parent().siblings().find('[add_people_id=post]').val()
            var major = $(this).parent().siblings().find('[add_people_id=major]').val()
            // var remark = $(this).parent().siblings().find('[add_people_id=remark]').val()

            if(name == '' || sex == '' || num == '' || department == '' || type == '' || education == '' || employType == '' ){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }

            let operation_id = window.sessionStorage['data-index']
            let that = $(this)
            $.ajax({
                url:'/Admin/Org/editStaff',
                type:'post',
                data:{
                    id:operation_id,
                    staff_name:name,
                    staff_sn:num,
                    sex:sex,
                    staff_title:post||'',
                    staff_emp:employType,
                    dep_id:department,
                    staff_type:type,
                    staff_xueli:education,
                    staff_zhicheng:title||'',
                    staff_zy:major||''
                    // staff_bz:remark||''
                },
                success:function(data){
                    console.log(data)
                    if(data.code==1){
                        var peopleTableHTML = $('#table_table').children('tbody').find(`[data-index=${window.sessionStorage['data-index']}]`)
                        peopleTableHTML.children('[data-name=name]').html(name)
                        peopleTableHTML.children('[data-name=sex]').html(sex)
                        peopleTableHTML.children('[data-name=num]').html(num)
                        peopleTableHTML.children('[data-name=department]').html(departmentText)
                        peopleTableHTML.children('[data-name=type]').html(type)
                        peopleTableHTML.children('[data-name=education]').html(education)
                        peopleTableHTML.children('[data-name=employType]').html(employType)
                        peopleTableHTML.children('[data-name=title]').html(title||'-')
                        peopleTableHTML.children('[data-name=post]').html(post||'-')
                        peopleTableHTML.children('[data-name=major]').html(major||'-')
                        // peopleTableHTML.children('[data-name=remark]').html(remark||'-')
                        that.parent().parent().removeClass('active')
                    }
                }
            })

        }else{
            $(this).parent().parent().removeClass('active')
        }

    })

let msg = '文件正在上传，请勿重复点击！'

    // 上传文件
    $('#file_upload').change(function (e) {
        // let data = this.files;
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
        msg = '文件正在上传，请勿重复点击！'
        $(this).siblings('.import_tip_btn_box').find('button').removeAttr('lock')
    })

    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                //防止重复点击按钮
                if( $(this).attr('lock') ){
                    $('#yz_modal').html(msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
                $(this).attr('lock','lock')
                let that = this

                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : '/Admin/Org/importStaff',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        // console.log(data);
                        if(data.code==1){
                            $('#import').removeClass('active')
                            window.location.href = '/Admin/Org/show'//强制刷新
                            // $('#file_upload').val('')
                            $(that).removeAttr('lock')
                        }else{
                            // console.log(222)
                            // console.log($('#yz_modal').html())
                            msg = data.msg
                            $('#yz_modal').html(msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })
    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
                // $('#file_upload').val('')
            }
        }
    })

    //展开收起部门
    $('#showORhidden').click(function(e){
        "use strict";
        e.preventDefault();
        let box_id = $(this).attr('href')
        console.log($(this).html())
        // $(box_id).addClass('active')
        // console.log($(box_id).height())
        $(box_id).slideToggle()
        if($(this).html() == '收起部门列表'){
            $(this).html('展开部门列表')
            // $(box_id).slideDown()
        }else{
            $(this).html('收起部门列表')
            // $(box_id).slideUp()
        }
    })

        //拖动拖动拖动
        let add_box =document.getElementById("add_people");
        let edit_box =document.getElementById("operation");
        add_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='add_people'){
                ev =ev ||event;
                let oLeft = add_box.offsetLeft;
                let oTop = add_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                add_box.onmousemove =function (ev){
                    ev = ev || event;
                    add_box.style.left =ev.clientX-a+'px';
                    add_box.style.top =ev.clientY-b+'px';
                    $(add_box).css({'cursor':'default'})
                }
            }
            
        }
        edit_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='operation'){
                ev =ev ||event;
                let oLeft = edit_box.offsetLeft;
                let oTop = edit_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                edit_box.onmousemove =function (ev){
                    ev = ev || event;
                    edit_box.style.left =ev.clientX-a+'px';
                    edit_box.style.top =ev.clientY-b+'px';
                    $(edit_box).css({'cursor':'default'})
                }
            }
        
        }
        document.onmouseup = function(){
            add_box.onmousemove = function(){
                null;
            }
            edit_box.onmousemove = function(){
                null;
            }
        }

})